package com.higgins.eventtracking.client;

import java.util.Date;

import com.higgins.eventtracking.client.dto.FinanceStatsDTO;
import com.higgins.eventtracking.client.dto.HealthEventDTO;
import com.higgins.eventtracking.client.dto.HealthStatsDTO;
import com.higgins.eventtracking.client.dto.QuestionDTO;
import com.higgins.eventtracking.client.dto.TrackingEventDTO;
import com.higgins.eventtracking.shared.FieldVerifier;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ChangeEvent;
import com.google.gwt.event.dom.client.ChangeHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * Entry point classes define <code>onModuleLoad()</code>.
 */
public class ELearningTracking implements EntryPoint {
	/**
	 * The message displayed to the user when the server cannot be reached or
	 * returns an error.
	 */
	private static final String SERVER_ERROR = "An error occurred while "
			+ "attempting to contact the server. Please check your network "
			+ "connection and try again.";
	
	private final ListBox categoryList = new ListBox();
	private final TextBox questionText = new TextBox();
	private final TextBox answerText = new TextBox();
	private final TextBox falseAnswerOneText = new TextBox();
	private final TextBox falseAnswerTwoText = new TextBox();
	private final TextBox falseAnswerThreeText = new TextBox();
	private final TextBox falseAnswerFourText = new TextBox();
	private final Button sendButton = new Button("Add");

	private final Label errorLabel = new Label();
	
	private final DialogBox dialogBox = new DialogBox();
	private final Button closeButton = new Button("Close");
	private final Label textToServerLabel = new Label();
	private final HTML serverResponseLabel = new HTML();
	

	

	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final ELearningTrackingServiceAsync learningService = GWT
			.create(ELearningTrackingService.class);

	/**
	 * This is the entry point method.
	 */
	private void loadFields()	{
		loadCategoryFields();
		sendButton.addStyleName("sendButton");
		sendButton.setEnabled(true);
		sendButton.setFocus(true);
		RootPanel.get("categoryList").add(categoryList);
		RootPanel.get("questionText").add(questionText);
		RootPanel.get("answerText").add(answerText);
		RootPanel.get("falseAnswerOneText").add(falseAnswerOneText);
		RootPanel.get("falseAnswerTwoText").add(falseAnswerTwoText);
		RootPanel.get("falseAnswerThreeText").add(falseAnswerThreeText);
		RootPanel.get("falseAnswerFourText").add(falseAnswerFourText);
		RootPanel.get("sendButtonContainer").add(sendButton);
	}
	private void loadCategoryFields()	{
		categoryList.addItem("------");
		categoryList.addItem("Contract Law");
		categoryList.addItem("Commercial Law");
		categoryList.addItem("Companies Law");
		categoryList.addItem("History");
	}
	
	private void createDialogBox()	{
		// Create the popup dialog box
		dialogBox.setText("Remote Procedure Call");
		dialogBox.setAnimationEnabled(true);
		// We can set the id of a widget by accessing its Element
		closeButton.getElement().setId("closeButton");
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogVPanel");
		dialogVPanel.add(new HTML("<b>Sending name to the server:</b>"));
		dialogVPanel.add(textToServerLabel);
		dialogVPanel.add(new HTML("<br><b>Server replies:</b>"));
		dialogVPanel.add(serverResponseLabel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);

		// Add a handler to close the DialogBox
		closeButton.addClickHandler(new ClickHandler() {
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				sendButton.setEnabled(true);
				sendButton.setFocus(true);
			}
		});		
	}
	private String getSelectedCategory()	{
		Integer selectedCategory = categoryList.getSelectedIndex();
		return categoryList.getItemText(selectedCategory);
	}
	private void createHandlers()	{
		// Create a handler for the sendButton and nameField
		class MyHandler implements ClickHandler, KeyUpHandler {
			/**
			 * Fired when the user clicks on the sendButton.
			 */
			public void onClick(ClickEvent event) {
				sendQuestionToServer();
			}

			/**
			 * Fired when the user types in the nameField.
			 */
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					sendQuestionToServer();
				}
			}

			/**
			 * Send the name from the nameField to the server and wait for a response.
			 */
			private void sendQuestionToServer() {
				// First, we validate the input.
				errorLabel.setText("");
				
				QuestionDTO dto = new QuestionDTO(getSelectedCategory(), 
						questionText.getText(), answerText.getText(), falseAnswerOneText.getText(), 
						falseAnswerTwoText.getText(), falseAnswerThreeText.getText(), 
						falseAnswerFourText.getText());
				
				sendButton.setEnabled(false);
				learningService.insertQuestion(dto,
						new AsyncCallback<String>() {
							public void onFailure(Throwable caught) {
								// Show the RPC error message to the user
								dialogBox.setText("Remote Procedure Call - Failure");
								serverResponseLabel.addStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(SERVER_ERROR);
								dialogBox.center();
								dialogBox.setPopupPosition(0, 0);
								closeButton.setFocus(true);
							}

							public void onSuccess(String result) {
								dialogBox.setText("Remote Procedure Call");
								serverResponseLabel.removeStyleName("serverResponseLabelError");
								serverResponseLabel.setHTML(result);
								dialogBox.center();
								dialogBox.setPopupPosition(0, 0);
								closeButton.setFocus(true);
							}
						});
			}
		}	
		MyHandler handler = new MyHandler();
		sendButton.addClickHandler(handler);
	}
	
	public void onModuleLoad() {
		loadFields();
		createDialogBox();
		createHandlers();
	}
}
